<?php
include(dirname(__FILE__).'/top-bottom-share-data.php');
$GLOBALS['error_message'] = '';
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
$dt = new DataTable(constant('DB_TABLE_MEMBER'));
if ( $act == 'new' ) {
	$member_data = $_REQUEST;
	$member_data['password'] = md5($member_data['password']);
	$member_data['status'] = 'new';
	//check email taken
	$condition = sprintf("`email`='%s'",Util::sql_safe($member_data['email']));
	$row = $dt->get_row($condition);
	if ( !$row ) {
		$dt->save($member_data);
		//send out verify email
		send_out_reg_verify_email($member_data['email'],$member_data['password']);
		ob_clean();
		$url = 'menberc-success.php?id='.$member_data['id'];
		header('Location:'.$url);
		exit();
	}else{
		$GLOBALS['error_message'] = sprintf("%s已經被註冊，請選擇其他郵箱地址",$member_data['email']);
	}
}
function send_out_reg_verify_email($email,$password)
{
	$body = file_get_contents(constant ( 'APP_DIR' ) . '/config/reg_verify_emal_template.html');
	$link = constant('SITE_URL_PATH').'/menberc-email-verify.php';
	$code = 'email='.$email.'&password='.$password;
	$code = base64_encode($code);
	$link .= '?code='.$code;
	$body = preg_replace('/\[\[\$LINK\]\]/',$link,$body);
	
	$GLOBALS['APP_INSTANCE']->send_out_email($email,'班尼頓加入會員郵箱地址驗證',$body);
	
	
}
?>